package com.atguigu.controller;

import com.atguigu.model.hosp.HospitalSet;
import com.atguigu.result.Result;
import com.atguigu.service.HospitalSetService;
import com.atguigu.utils.MD5;
//import com.atguigu.utils.RedisKeyUtils;
import com.atguigu.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 对医院信息进行CRUD
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin//支持跨域访问
public class HospitalSetController /*implements RedisKeyUtils*/{
    @Autowired
    @Qualifier("hospitalSetService")
    private HospitalSetService hospitalSetService;

//    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;

    //1 查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("/findAll")
    public Result/*List<HospitalSet>*/ findAllHospitalSet() {
//        List<Object> list = redisTemplate.opsForList().range(HS, -1, -1);
//        List<HospitalSet> list0 = new ArrayList();
//        if(list != null && !list.isEmpty()){
//            for (Object o : list) {
//                list0.add((HospitalSet)o);
//            }
//            return Result.ok(list);
//        } else {
//            list0 = hospitalSetService.list(null);
//            Result.ok(list0);
//        }
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list(null);
        //返回结果信息，包含响应状态（200）、响应数据
        return Result.ok(list) /*list*/;
    }

    //2 逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    /*
        @RequestBody主要用来接收前端传递给后端的json字符串
    中的数据的(请求体中的数据的)，所以只能接收POST请求。

    controller中业务方法的集合参数获取，如果是集合参数，集合中需要封装一个实体类。

    参数属性名与实体类中请求参数名称一致，参数值会自动映射匹配

    required属性：请求正文中是否必须有值，默认为true
    */
    //3 条件查询带分页
    @ApiOperation(value = "分页查询")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);

        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号

        //sql语句的条件值都封装在vo类中
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }
        //调用方法实现分页查询
        IPage<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        //返回结果
        return Result.ok(pageHospitalSet);
    }

    //4 添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥（对秘钥进行MD5加密）
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()
                + "" + random.nextInt(1000)));
        //创建时间和更新时间在数据库中已设置成自动填充
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save) {
//            redisTemplate.delete(HS);
//            redisTemplate.opsForSet().add(HS, hospitalSet);
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取医院设置
    @ApiOperation(value = "获取当前医院id的医院设置")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //8 医院设置锁定和解锁（接收某个用户的id以及状态，修改医院的状态）
    @ApiOperation(value = "医院设置的锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态（status为1时解锁，为0时被锁定）
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //9 发送签名秘钥
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);

//        String signKey = hospitalSet.getSignKey();
//
//        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }



}
